package com.ruoyi.legal.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.legal.domain.vo.XstzBatchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.legal.domain.Xstz;
import com.ruoyi.legal.service.IXstzService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 线索台账Controller
 * 
 * @author xwj
 * @date 2025-04-22
 */
@RestController
@RequestMapping("/legal/xstz")
@Api(tags = "线索台账")
public class XstzController extends BaseController {
    @Autowired
    private IXstzService xstzService;

    /**
     * 查询线索台账列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody Xstz xstz) {
        startPage();
        Long userId = getUserId();
        xstz.setCreateId(userId);
        List<Xstz> list = xstzService.selectXstzList(xstz);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody Xstz xstz) {
        startOrderBy();
        List<Xstz> list = xstzService.selectXstzList(xstz);
        return success(list);
    }

    /**
     * 导出线索台账列表
     */
    @Log(title = "线索台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, Xstz xstz) {
        List<Xstz> list = xstzService.selectXstzList(xstz);
        ExcelUtil<Xstz> util = new ExcelUtil<Xstz>(Xstz.class);
        util.exportExcel(response, list, "线索台账数据");
    }

    /**
     * 获取线索台账详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id)  {
        return success(xstzService.selectXstzById(id));
    }

    @PostMapping("/batchSaveUpdate")
    @ApiOperation("批量保存")
    public ResponseModel<XstzBatchVo> batchSaveUpdate(@RequestBody XstzBatchVo xstzBatchVo) {
        SysUser user = getLoginUser().getUser();
        xstzService.batchSaveXstzBatch(xstzBatchVo, user);

        return ResponseModel.of(xstzBatchVo);
    }

    /**
     * 新增线索台账
     */
    @Log(title = "线索台账", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody Xstz xstz) {
        SysUser user = getLoginUser().getUser();
        if(xstz.getId() == null) {
        xstz.setCreateId(user.getUserId());
            xstz.setStatus("0");
            xstz.setCreateTime(DateUtils.getNowDate());
            xstz.setCreateBy(user.getNickName());
            xstzService.insertXstz(xstz);
        } else {
            xstz.setUpdateId(user.getUserId());
            xstz.setUpdateTime(DateUtils.getNowDate());
            xstz.setUpdateBy(user.getNickName());
            xstzService.updateXstz(xstz);
        }

        return success(xstz);
    }

    /**
     * 修改线索台账
     */
    @Log(title = "线索台账", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody Xstz xstz) {
        SysUser user = getLoginUser().getUser();
                xstz.setUpdateId(user.getUserId());
                xstz.setUpdateTime(DateUtils.getNowDate());
                xstz.setUpdateBy(user.getNickName());
        return success(xstzService.updateXstz(xstz));
    }

    /**
     * 删除线索台账
     */
    @Log(title = "线索台账", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(xstzService.deleteXstzByIds(ids));
    }

    @Log(title = "线索台账", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody Xstz xstz) {
        int num = xstzService.delete(xstz);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<Xstz>> queryPage(@RequestBody Xstz xstz) {
        Pages<Xstz> page = xstzService.queryPage(xstz);
        return ResponseModel.of(page);
    }
}
